package com.boatAnchorAssistant.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.User;
import com.boatAnchorAssistant.pojo.entity.UserOperationLog;
import com.boatAnchorAssistant.pojo.param.userOperationLog.*;
import com.boatAnchorAssistant.pojo.vo.userOperationLog.*;
import com.boatAnchorAssistant.pojo.dto.userOperationLog.*;
import com.boatAnchorAssistant.service.repository.in.UserOperationLogRepository;
import com.boatAnchorAssistant.service.repository.in.UserRepository;
import com.boatAnchorAssistant.service.service.in.UserOperationLogService;
import com.boatAnchorAssistant.service.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import nirvana.core.domains.dto.OperationLogDto;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：用户操作日志服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 *
 */
@Slf4j
@Service
public class UserOperationLogServiceImpl implements UserOperationLogService {

    private final UserOperationLogRepository userOperationLogRepository;

    private final UserRepository userRepository;
    private final StringRedisTemplate stringRedisTemplate;

    public UserOperationLogServiceImpl(UserOperationLogRepository userOperationLogRepository, UserRepository userRepository, StringRedisTemplate stringRedisTemplate) {
        this.userOperationLogRepository = userOperationLogRepository;
        this.userRepository = userRepository;
        this.stringRedisTemplate = stringRedisTemplate;
    }



    /**
     * 获取用户操作日志 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetUserOperationLogListVo> getUserOperationLogList(GetUserOperationLogListParam param) {

        SelectUserOperationLogDto dto = new SelectUserOperationLogDto();
        BeanUtils.copyProperties(param, dto);
        Page<UserOperationLog> page = userOperationLogRepository.selectUserOperationLog(dto);
        List<UserOperationLog> userOperationLogList = page.getRecords();

        List<Long> userIds = userOperationLogList.stream()
                .map(UserOperationLog::getUserId)
                .collect(Collectors.toList());

        Map<Long, String> userNameMap = userRepository.getListByIds(userIds)
                .stream()
                .collect(Collectors.toMap(User::getId, User::getName));

        List<GetUserOperationLogListVo> list = new ArrayList<>();
        for (UserOperationLog userOperationLog : userOperationLogList) {
            GetUserOperationLogListVo vo = new GetUserOperationLogListVo();
            BeanUtils.copyProperties(userOperationLog, vo);

            Long userId = userOperationLog.getUserId();
            vo.setName(userNameMap.getOrDefault(userId,""));

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取操作类型下拉列表
     *
     * @return
     */
    @Override
    public List<GetOperationTypeListVo> getOperationTypeList() {
        return userOperationLogRepository.getOperationTypeList();
    }

    /**
     * 记录操作日志
     */
    @Override
    public void recordOperationLog() {
        log.info("---开始处理操作日志---");
        ListOperations<String, String> list = stringRedisTemplate.opsForList();
        String json = "";



        while (true) {
            json = list.rightPop("OPERATION_RBAC_LOG_LIST");
            if (!StringUtils.hasText(json)) {
                return;
            }
            OperationLogDto logDto = JsonUtil.toBean(json, OperationLogDto.class);
            UserOperationLog operationLog = new UserOperationLog();
            BeanUtils.copyProperties(logDto, operationLog);
            userOperationLogRepository.create(operationLog);
        }

    }




}



